<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Merge (SQL)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Merge_(SQL)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Merge_SQL rootpage-Merge_SQL skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Merge (SQL)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>A <a href="Relational_database_management_system" class="mw-redirect" title="Relational database management system">relational database management system</a> uses <a href="SQL" title="SQL">SQL</a> <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">MERGE</code> (also called <i>upsert</i>) statements to <code><a href="Insert_(SQL)" title="Insert (SQL)">INSERT</a></code> new records or <code><a href="Update_(SQL)" title="Update (SQL)">UPDATE</a></code> or <code><a href="Delete_(SQL)" title="Delete (SQL)">DELETE</a></code> existing records depending on whether <a href="Condition_(SQL)" title="Condition (SQL)">condition</a> matches. It was officially introduced in the <a href="SQL%3A2003" title="SQL:2003">SQL:2003</a> standard, and expanded in the <a href="SQL%3A2008" title="SQL:2008">SQL:2008</a> standard.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Usage">Usage</h2></div>
<div class="mw-highlight mw-highlight-lang-tsql mw-content-ltr" dir="ltr"><pre><span class="k">MERGE</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">tablename</span><span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="n">table_reference</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="p">(</span><span class="k">condition</span><span class="p">)</span>
<span class="w"> </span><span class="k">WHEN</span><span class="w"> </span><span class="n">MATCHED</span><span class="w"> </span><span class="k">THEN</span>
<span class="w"> </span><span class="k">UPDATE</span><span class="w"> </span><span class="k">SET</span><span class="w"> </span><span class="n">column1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">value1</span><span class="w"> </span><span class="o">[</span><span class="n">, column2 = value2 ...</span><span class="o">]</span>
<span class="w"> </span><span class="k">WHEN</span><span class="w"> </span><span class="ow">NOT</span><span class="w"> </span><span class="n">MATCHED</span><span class="w"> </span><span class="k">THEN</span>
<span class="w"> </span><span class="k">INSERT</span><span class="w"> </span><span class="p">(</span><span class="n">column1</span><span class="w"> </span><span class="o">[</span><span class="n">, column2 ...</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="n">value1</span><span class="w"> </span><span class="o">[</span><span class="n">, value2 ...</span><span class="o">]</span><span class="p">);</span>
</pre></div>
<p>A <a href="Join_(SQL)#Right_outer_join" title="Join (SQL)">right join</a> is employed over the Target (the INTO table) and the Source (the USING table / view / sub-query)--where Target is the left table and Source is the right one. The four possible combinations yield these rules:
</p>
<ul><li>If the ON field(s) in the Source matches the ON field(s) in the Target, then UPDATE</li>
<li>If the ON field(s) in the Source does not match the ON field(s) in the Target, then INSERT</li>
<li>If the ON field(s) does not exist in the Source but does exist in the Target, then no action is performed.</li>
<li>If the ON field(s) does not exist in either the Source or Target, then no action is performed.</li></ul>
<p>If multiple Source rows match a given Target row, an error is mandated by SQL:2003 standards. You cannot update a Target row multiple times with a MERGE statement
</p>
<div class="mw-heading mw-heading2"><h2 id="Implementations">Implementations</h2></div>
<p>Database management systems <a href="PostgreSQL" title="PostgreSQL">PostgreSQL</a>,<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> <a href="Oracle_Database" title="Oracle Database">Oracle Database</a>, <a href="IBM_Db2" title="IBM Db2">IBM Db2</a>, <a href="Teradata" title="Teradata">Teradata</a>, <a href="EXASOL" class="mw-redirect" title="EXASOL">EXASOL</a>, <a href="Firebird_(database_server)" title="Firebird (database server)">Firebird</a>, <a href="CUBRID" title="CUBRID">CUBRID</a>, <a href="H2_(DBMS)" class="mw-redirect" title="H2 (DBMS)">H2</a>, <a href="HSQLDB" title="HSQLDB">HSQLDB</a>, <a href="MS_SQL" class="mw-redirect" title="MS SQL">MS SQL</a>, <a href="MonetDB" title="MonetDB">MonetDB</a>, <a href="Vectorwise" class="mw-redirect" title="Vectorwise">Vectorwise</a> and <a href="Apache_Derby" title="Apache Derby">Apache Derby</a> support the standard syntax. Some also add non-standard SQL extensions.
</p>
<div class="mw-heading mw-heading3"><h3 id="Synonymous"> Synonymous</h3></div>
<p>Some database implementations adopted the term <i><b>upsert</b></i> (a <a href="Portmanteau" class="mw-redirect" title="Portmanteau">portmanteau</a> of <i>update</i> and <i>insert</i>) to a <a href="Database" title="Database">database</a> statement, or combination of statements, that inserts a record to a table in a database if the record does not exist or, if the record already exists, updates the existing record. This synonym is used in <a href="PostgreSQL" title="PostgreSQL">PostgreSQL</a> (v9.5+)<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> and <a href="SQLite" title="SQLite">SQLite</a> (v3.24+).<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> It is also used to abbreviate the "MERGE" equivalent pseudo-code.
</p><p>It is used in <a href="Microsoft_Azure_SQL_Database" title="Microsoft Azure SQL Database">Microsoft Azure SQL Database</a>.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Other_non-standard_implementations">Other non-standard implementations</h3></div>
<p>Some other database management systems support this, or very similar behavior, through their own, non-standard SQL extensions.
</p><p><a href="MySQL" title="MySQL">MySQL</a>, for example, supports the use of <code class="mw-highlight mw-highlight-lang-mysql mw-content-ltr" style="" dir="ltr"><span class="k">INSERT</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="k">DUPLICATE</span><span class="w"> </span><span class="k">KEY</span><span class="w"> </span><span class="k">UPDATE</span></code> syntax<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> which can be used to achieve a similar effect with the limitation that the join between target and source has to be made only on PRIMARY KEY or UNIQUE constraints, which is not required in the ANSI/ISO standard. It also supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">>REPLACE INTO</code> syntax,<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> which first attempts an insert, and if that fails, deletes the row, if exists, and then inserts the new one. There is also an <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">IGNORE</code> clause for the <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">INSERT</code> statement,<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> which tells the server to ignore "duplicate key" errors and go on (existing rows will not be inserted or updated, but all new rows will be inserted).
</p><p><a href="SQLite" title="SQLite">SQLite</a>'s <code class="mw-highlight mw-highlight-lang-sql mw-content-ltr" style="" dir="ltr"><span class="k">INSERT</span><span class="w"> </span><span class="k">OR</span><span class="w"> </span><span class="k">REPLACE</span><span class="w"> </span><span class="k">INTO</span></code> works similarly. It also supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">REPLACE INTO</code> as an alias for compatibility with MySQL.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup>
</p><p><a href="Firebird_(database_server)" title="Firebird (database server)">Firebird</a> supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">MERGE INTO</code> though fails to throw an error when there are multiple Source data rows. Additionally there is a single-row version, <code class="mw-highlight mw-highlight-lang-sql mw-content-ltr" style="" dir="ltr"><span class="k">UPDATE</span><span class="w"> </span><span class="k">OR</span><span class="w"> </span><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">tablename</span><span class="w"> </span><span class="p">(</span><span class="n">columns</span><span class="p">)</span><span class="w"> </span><span class="k">VALUES</span><span class="w"> </span><span class="p">(</span><span class="k">values</span><span class="p">)</span><span class="w"> </span><span class="p">[</span><span class="n">MATCHING</span><span class="w"> </span><span class="p">(</span><span class="n">columns</span><span class="p">)]</span></code>, but the latter does not give you the option to take different actions on insert versus update (e.g. setting a new sequence value only for new rows, not for existing ones.)
</p><p><a href="IBM_Db2" title="IBM Db2">IBM Db2</a> extends the syntax with multiple <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">WHEN MATCHED</code> and <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">WHEN NOT MATCHED</code> clauses, distinguishing them with <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">... AND some-condition</code> <a href="Guard_(computing)" class="mw-redirect" title="Guard (computing)">guards</a>.
</p><p><a href="Microsoft_SQL_Server" title="Microsoft SQL Server">Microsoft SQL Server</a> extends with supporting guards and also with supporting Left Join via <code class="mw-highlight mw-highlight-lang-tsql mw-content-ltr" style="" dir="ltr"><span class="k">WHEN</span><span class="w"> </span><span class="ow">NOT</span><span class="w"> </span><span class="n">MATCHED</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">SOURCE</span></code> clauses.
</p><p><a href="PostgreSQL" title="PostgreSQL">PostgreSQL</a> supports merge since version 15 but previously supported merging via <code class="mw-highlight mw-highlight-lang-postgresql mw-content-ltr" style="" dir="ltr"><span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="mf">...</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="k">CONFLICT</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="n">conflict_target</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="n">conflict_action</span></code>.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>
</p><p><a href="CUBRID" title="CUBRID">CUBRID</a> supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">MERGE INTO</code><sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> statement. And supports the use of <code class="mw-highlight mw-highlight-lang-mysql mw-content-ltr" style="" dir="ltr"><span class="k">INSERT</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="k">DUPLICATE</span><span class="w"> </span><span class="k">KEY</span><span class="w"> </span><span class="k">UPDATE</span></code> syntax.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> It also supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">REPLACE INTO</code> for compatibility with MySQL.<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p><p><a href="Apache_Phoenix" title="Apache Phoenix">Apache Phoenix</a> supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">UPSERT VALUES</code><sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> and <code>UPSERT SELECT</code><sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> syntax.
</p><p><a href="Apache_Spark#Spark_SQL" title="Apache Spark">Spark SQL</a> supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">UPDATE SET *</code> and <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">INSERT *</code> clauses in actions.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p><p><a href="Apache_Impala" title="Apache Impala">Apache Impala</a> supports <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">UPSERT INTO ... SELECT</code>.<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Usage_in_NoSQL">Usage in NoSQL</h2></div>
<p>A similar concept is applied in some <a href="NoSQL" title="NoSQL">NoSQL</a> databases.
</p><p>In <a href="MongoDB" title="MongoDB">MongoDB</a> the fields in a value associated with a key can be updated with an <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">update</code> operation. The <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">update</code> raises an error if the key is not found. In the <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">update</code> operation it is possible to set the <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">upsert</code> flag: in this case a new value is stored associated to the given key if it does not exist, otherwise the whole value is replaced.
</p><p>In <a href="Redis" title="Redis">Redis</a> the <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">SET</code> operations sets the value associated with a given key. Redis does not know any detail of the internal structure of the value, so an <i>update</i> would have no meaning. So the <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">SET</code> operation has always a <i>set or replace</i> semantics.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li>Join in particular:
<ul><li><a href="Join_(SQL)" title="Join (SQL)">Join (SQL)</a></li>
<li><a href="Join_(Unix)" title="Join (Unix)">join (Unix)</a></li></ul></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.postgresql.org/docs/15/release-15.html">"E.1. Release 15"</a>. <i>PostgreSQL Documentation</i>. 13 October 2022. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20221013141239/https://www.postgresql.org/docs/15/release-15.html">Archived</a> from the original on 13 October 2022<span class="reference-accessdate">. Retrieved <span class="nowrap">13 October</span> 2022</span>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.postgresqltutorial.com/postgresql-upsert/">"PostgreSQL Upsert Using INSERT ON CONFLICT statement"</a>. <i>PostgreSQL Tutorial</i>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20221128173803/https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-upsert/">Archived</a> from the original on Nov 28, 2022.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text">"<a rel="nofollow" class="external text" href="http://sqlite.org/lang_UPSERT.html">upsert</a>", SQLite, visited 6-6-2018.</span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://msdn.microsoft.com/en-us/library/bb510625.aspx">"MERGE (Transact-SQL)"</a>. <i>Transact-SQL Reference (Database Engine)</i>. Microsoft Learn. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20160624122818/https://msdn.microsoft.com/en-us/library/bb510625.aspx">Archived</a> from the original on Jun 24, 2016.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html">MySQL :: MySQL 5.1 Reference Manual :: 12.2.4.3 INSERT ... ON DUPLICATE KEY UPDATE Syntax</a></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://dev.mysql.com/doc/refman/5.1/en/replace.html">MySQL 5.1 Reference Manual: 11.2.6 REPLACE Syntax</a></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://dev.mysql.com/doc/refman/5.5/en/insert.html">"MySQL 5.5 Reference Manual :: 13.2.5 INSERT Syntax"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">29 October</span> 2013</span>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.sqlite.org/lang_insert.html">"SQL As Understood By SQLite: INSERT"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2012-09-27</span></span>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.postgresql.org/docs/current/static/sql-insert.html">PostgreSQL INSERT page </a></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.cubrid.org/blog/news/announcing-cubrid-9-0-with-3x-performance-increase-and-sharding-support">"New CUBRID 9.0.0"</a>. CUBRID Official Blog. 2012-10-30<span class="reference-accessdate">. Retrieved <span class="nowrap">2012-11-08</span></span>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.cubrid.org/manual/10_0/en/sql/query/insert.html#on-duplicate-key-update-clause">CUBRID :: Data Manipulation Statements :: Insert :: ON DUPLICATE KEY UPDATE Clause</a></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.cubrid.org/manual/10_0/en/sql/function/string_fn.html#replace">CUBRID :: Data Manipulation Statements :: Replace</a></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://phoenix.apache.org/language/#upsert_values">"UPSERT VALUES"</a>.</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://phoenix.apache.org/language/#upsert_select">"UPSERT SELECT"</a>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.databricks.com/spark/latest/spark-sql/language-manual/delta-merge-into.html">"MERGE INTO (Delta Lake on Databricks)"</a>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://impala.apache.org/docs/build/html/topics/impala_upsert.html">"UPSERT Statement (Apache Impala Documentation)"</a>.</cite></span>
</li>
</ol></div>
<ul><li><cite id="CITEREFHsuObe2008" class="citation web cs1">Hsu, Leo; Obe, Regina (May 18, 2008). <a rel="nofollow" class="external text" href="http://www.postgresonline.com/journal/archives/51-Cross-Compare-of-SQL-Server,-MySQL,-and-PostgreSQL.html">"Cross Compare of SQL Server, MySQL, and PostgreSQL"</a>. <i>Postgres OnLine Journal</i><span class="reference-accessdate">. Retrieved <span class="nowrap">8 October</span> 2010</span>.</cite></li>
<li><cite id="CITEREFChodorowMike_Dirolf2010" class="citation book cs1">Chodorow, Kristina; Mike Dirolf (September 2010). <i>MongoDB: The Definitive Guide</i>. <a href="O'Reilly_Media" title="O'Reilly Media">O'Reilly</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-449-38156-1</bdi>.</cite></li></ul>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_9016.htm#SQLRF01606">Oracle 11g Release 2 documentation</a> on <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">MERGE</code></li>
<li><a rel="nofollow" class="external text" href="http://www.firebirdsql.org/refdocs/langrefupd21-merge.html">Firebird 2.1 documentation</a> on <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">MERGE</code></li>
<li><a rel="nofollow" class="external text" href="http://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.sql.ref.doc/doc/r0010873.html">DB2 v9 MERGE statement</a></li>
<li><a rel="nofollow" class="external text" href="http://msdn.microsoft.com/en-us/library/bb510625.aspx">Microsoft SQL Server documentation</a></li>
<li><a rel="nofollow" class="external text" href="http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_merge_statement">HSQLdb 2.0 Data Change Statements</a></li>
<li><a rel="nofollow" class="external text" href="http://www.h2database.com/html/grammar.html#merge">H2 (1.2) SQL Syntax page</a></li></ul>
<div class="navbox-styles"><style data-mw-deduplicate="TemplateStyles:r1129693374">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" ยท ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1236075235">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbox{box-sizing:border-box;border:1px solid #a2a9b1;width:100%;clear:both;font-size:88%;text-align:center;padding:1px;margin:1em auto 0}.mw-parser-output .navbox .navbox{margin-top:0}.mw-parser-output .navbox+.navbox,.mw-parser-output .navbox+.navbox-styles+.navbox{margin-top:-1px}.mw-parser-output .navbox-inner,.mw-parser-output .navbox-subgroup{width:100%}.mw-parser-output .navbox-group,.mw-parser-output .navbox-title,.mw-parser-output .navbox-abovebelow{padding:0.25em 1em;line-height:1.5em;text-align:center}.mw-parser-output .navbox-group{white-space:nowrap;text-align:right}.mw-parser-output .navbox,.mw-parser-output .navbox-subgroup{background-color:#fdfdfd}.mw-parser-output .navbox-list{line-height:1.5em;border-color:#fdfdfd}.mw-parser-output .navbox-list-with-group{text-align:left;border-left-width:2px;border-left-style:solid}.mw-parser-output tr+tr>.navbox-abovebelow,.mw-parser-output tr+tr>.navbox-group,.mw-parser-output tr+tr>.navbox-image,.mw-parser-output tr+tr>.navbox-list{border-top:2px solid #fdfdfd}.mw-parser-output .navbox-title{background-color:#ccf}.mw-parser-output .navbox-abovebelow,.mw-parser-output .navbox-group,.mw-parser-output .navbox-subgroup .navbox-title{background-color:#ddf}.mw-parser-output .navbox-subgroup .navbox-group,.mw-parser-output .navbox-subgroup .navbox-abovebelow{background-color:#e6e6ff}.mw-parser-output .navbox-even{background-color:#f7f7f7}.mw-parser-output .navbox-odd{background-color:transparent}.mw-parser-output .navbox .hlist td dl,.mw-parser-output .navbox .hlist td ol,.mw-parser-output .navbox .hlist td ul,.mw-parser-output .navbox td.hlist dl,.mw-parser-output .navbox td.hlist ol,.mw-parser-output .navbox td.hlist ul{padding:0.125em 0}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}body.skin--responsive .mw-parser-output .navbox-image img{max-width:none!important}@media print{body.ns-0 .mw-parser-output .navbox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r920966791">
/* start https://en.wikipedia.org/ */
.mw-parser-output span.smallcaps{font-variant:small-caps}.mw-parser-output span.smallcaps-smaller{font-size:85%}
/* end https://en.wikipedia.org/ */
</style></div><div role="navigation" class="navbox" aria-labelledby="SQL70" style="padding:3px"><table class="nowraplinks mw-collapsible autocollapse navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><style data-mw-deduplicate="TemplateStyles:r1239400231">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div id="SQL70" style="font-size:114%;margin:0 4em"><a href="SQL" title="SQL">SQL</a></div></th></tr><tr><th scope="row" class="navbox-group" style="width:1%">Versions</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="SEQUEL" class="mw-redirect" title="SEQUEL">SEQUEL</a></li>
<li>SQL-86</li>
<li>SQL-89</li>
<li><a href="SQL-92" title="SQL-92">SQL-92</a></li>
<li><a href="SQL%3A1999" title="SQL:1999">SQL:1999</a></li>
<li><a href="SQL%3A2003" title="SQL:2003">SQL:2003</a></li>
<li><a href="SQL%3A2006" title="SQL:2006">SQL:2006</a></li>
<li><a href="SQL%3A2008" title="SQL:2008">SQL:2008</a></li>
<li><a href="SQL%3A2011" title="SQL:2011">SQL:2011</a></li>
<li><a href="SQL%3A2016" title="SQL:2016">SQL:2016</a></li>
<li><a href="SQL%3A2023" title="SQL:2023">SQL:2023</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="SQL_reserved_words" class="mw-redirect" title="SQL reserved words">Keywords</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><span class="smallcaps"><a href="Alias_(SQL)" title="Alias (SQL)">As</a></span></li>
<li><span class="smallcaps"><a href="Case_(SQL)" class="mw-redirect" title="Case (SQL)">Case</a></span></li>
<li><span class="smallcaps"><a href="Create_(SQL)" class="mw-redirect" title="Create (SQL)">Create</a></span></li>
<li><span class="smallcaps"><a href="Delete_(SQL)" title="Delete (SQL)">Delete</a></span></li>
<li><span class="smallcaps"><a href="From_(SQL)" title="From (SQL)">From</a></span></li>
<li><span class="smallcaps"><a href="Group_by_(SQL)" title="Group by (SQL)">Group by</a></span></li>
<li><span class="smallcaps"><a href="Having_(SQL)" title="Having (SQL)">Having</a></span></li>
<li><span class="smallcaps"><a href="Insert_(SQL)" title="Insert (SQL)">Insert</a></span></li>
<li><span class="smallcaps"><a href="Join_(SQL)" title="Join (SQL)">Join</a></span></li>
<li></li>
<li><span class="smallcaps"><a href="Null_(SQL)" title="Null (SQL)">Null</a></span></li>
<li><span class="smallcaps"><a href="Order_by" title="Order by">Order by</a></span></li>
<li><span class="smallcaps"><a href="Window_function_(SQL)" title="Window function (SQL)">Over</a></span></li>
<li><span class="smallcaps"><a href="Prepare_(SQL)" class="mw-redirect" title="Prepare (SQL)">Prepare</a></span></li>
<li><span class="smallcaps"><a href="Select_(SQL)" title="Select (SQL)">Select</a></span></li>
<li><span class="smallcaps"><a href="Truncate_(SQL)" title="Truncate (SQL)">Truncate</a></span></li>
<li><span class="smallcaps"><a href="Set_operations_(SQL)" title="Set operations (SQL)">Union</a></span></li>
<li><span class="smallcaps"><a href="Update_(SQL)" title="Update (SQL)">Update</a></span></li>
<li><span class="smallcaps"><a href="With_(SQL)" class="mw-redirect" title="With (SQL)">With</a></span></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Related</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Edgar_F._Codd" title="Edgar F. Codd">Edgar Codd</a></li>
<li><a href="Relational_database" title="Relational database">Relational database</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">ISO/IEC SQL parts</th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li>Framework</li>
<li>Foundation</li>
<li><a href="SQL/CLI" class="mw-redirect" title="SQL/CLI">Call-Level Interface</a></li>
<li><a href="SQL/PSM" title="SQL/PSM">Persistent Stored Modules</a></li>
<li><a href="SQL/MED" title="SQL/MED">Management of External Data</a></li>
<li><a href="SQL/OLB" title="SQL/OLB">Object Language Bindings</a></li>
<li><a href="SQL/Schemata" title="SQL/Schemata">Information and Definition Schemas</a></li>
<li><a href="SQL/JRT" title="SQL/JRT">SQL Routines and Types for the Java Programming Language</a></li>
<li><a href="SQL/XML" title="SQL/XML">XML-Related Specifications</a></li></ul>
</div></td></tr></tbody></table></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-03-31" href="https://en.wikipedia.org/wiki/?title=Merge_(SQL)&oldid=1283331315">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>